// pages/address/address.js
var utils = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */

  data: {
    addressList: [],
    iftrue:false
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var self = this;
    var userInfo = wx.getStorageSync("userinfo");
    let serverUrl = app.globalData.serverUrl + '/getAddressListByOpenId?openId=' + userInfo.openId;
    wx.request({
      url: serverUrl,
      method: 'GET',
      success: function(result) {
        console.log(result);
        var list = [];
        for (let address of result.data.data) {
          list.push({
            "id": address.id,
            "name": address.receiveName,
            "mobile": address.telephone,
            "regionInfo": address.regionInfo,
            "address": address.detailAddress,
            "area": "60-90",
            "image": "../../images/uncheck.png"
          })
        }

        self.setData({
          addressList: list
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  addAddress: function() {
    wx.navigateTo({
      url: '../address/address',
    })
  },
  delAddress: function(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function(sm) {
        if (sm.confirm) {
          let self = this;
          let id = e.currentTarget.dataset.id;
          let index = e.currentTarget.dataset.index;
          let serverUrl = app.globalData.serverUrl + '/deleteAddress?addressId=' + id;
          console.log(e);
          wx.request({
            url: serverUrl,
            method: 'GET',
            success: function(result) {
              console.log(result);

              var list = self.data.addressList;
              list.splice(index, 1);
              self.setData({
                addressList: list
              })
            }
          })
        }
      }
    })
  },
  longPress: function(e) {
    console.log("长按。。。。");
    this.setData({
      iftrue:true
    })
  },
  onPullDownRefresh: function() {
    console.log("下拉。。。。");
  },
  selectAddress:function(e){
    console.log("选择地址。。。。");
    var newAddress={};
    if (e.currentTarget.dataset){
      newAddress.name = e.currentTarget.dataset.address.name;
      newAddress.telephone = e.currentTarget.dataset.address.mobile;
      newAddress.detail = e.currentTarget.dataset.address.regionInfo;
    }
    //微信小程序从子页面退回父页面时的数据传递 wx.navigateBack()
    var pages = getCurrentPages();
    var orderConfirmPage = pages[pages.length - 2];  //上一个页面
    orderConfirmPage.setData({
      address: newAddress
    })
    wx.navigateBack({
      delta:1
    })
  }
})